package com.zyh.system.JDsupermarkt.dao.impl;

import com.zyh.system.JDsupermarkt.dao.GoodsDao;
import com.zyh.system.JDsupermarkt.domain.Goods;

import java.io.*;
import java.util.LinkedHashMap;
import java.util.Set;

/**
 * @Author zyh
 * @Date 2024/8/2 17:10
 * @Description GoodSDao 实现类
 */

public class GoodsDaoImpl implements GoodsDao {
    public static final LinkedHashMap<Integer, Goods> GOODS_MAP = new LinkedHashMap<>();
    public static final String FILE_PATH = "D:\\Code\\IDEA\\idea_projects\\JavaSE-plus\\day09-code\\src\\com\\zyh\\system\\JDsupermarkt\\resources\\GoodsDao.txt";

    static {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(FILE_PATH));
            String line = null;
            while ((line = br.readLine()) != null) {
                String[] split = line.split("-");
                for (String s : split) {
                    GOODS_MAP.put(Integer.parseInt(split[0]), new Goods(split[1], Double.parseDouble(split[2]), split[3], Double.parseDouble(split[4]), Integer.parseInt(split[5])));
                }
            }
        } catch (IOException e) {
            System.out.println("文件路径错误");
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        /*GOODS_MAP.put(3333, new Goods("ThinkPad x240", 5939.0, "黑色", 12.2, 50));
        GOODS_MAP.put(2222, new Goods("iPhone 7plus", 5686.0, "白色", 5.2, 5000));*/
    }

    /**
     * 添加商品
     *
     * @param map     集合
     * @param goods   商品对象
     * @param goodsId 商品编号
     * @return 结束方法
     */
    @Override
    public boolean addGoods(LinkedHashMap<Integer, Goods> map, Goods goods, Integer goodsId) {
        map.put(goodsId, goods);
        insertGoods(goods,goodsId);
        return true;
    }

    /**
     * 展示所有商品
     *
     * @param map Goods 集合
     */
    @Override
    public void showAll(LinkedHashMap<Integer, Goods> map) {
        System.out.println("编号\t商品名称\t\t\t价格\t颜色\t尺寸\t库存");
        for (Integer key : map.keySet()) {
            Goods value = map.get(key);
            System.out.println(key + "\t" + value.getGoodsName() + "\t\t" + value.getGoodsPrice() + "\t" + value.getGoodsColor() + "\t" + value.getGoodsSize() + "\t" + value.getStock());
        }
    }

    /**
     * 根据id查询对应商品
     *
     * @param map Goods集合
     */
    @Override
    public Goods selectById(LinkedHashMap<Integer, Goods> map, Integer id) {
        Goods goods = null;
        Set<Integer> keySet = map.keySet();
        for (Integer key : keySet) {
            if (key.equals(id)) {
                Goods value = map.get(key);
                goods = value;
                break;
            }
        }
        return goods;
    }

    /**
     * 根据id修改商品
     *
     * @param map Goods集合
     * @param id  商品id
     * @return 结束方法
     */
    @Override
    public boolean updateById(LinkedHashMap<Integer, Goods> map, Integer id, Goods goods) throws IOException {
        Goods goods1 = selectById(map, id);
        if (goods1 != null) {
            map.put(id, goods);
            /*//文件相关操作
            ArrayList<String> list=new ArrayList<>();
            BufferedWriter bw=new BufferedWriter(new FileWriter("D:\\course\\02-JavaSE进阶\\day09-IO流\\作业\\IO流版：京东商城\\GoodsDao.txt"));
            BufferedReader br=new BufferedReader(new FileReader("D:\\course\\02-JavaSE进阶\\day09-IO流\\作业\\IO流版：京东商城\\GoodsDao.txt"));
            String line=null;
            String strId=id+"";
            while ((line=br.readLine())!=null){
                list.add(line);
            }
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).contains(strId)){
                    list.remove(i);
                    break;
                }
            }
            File file=new File("D:\\course\\02-JavaSE进阶\\day09-IO流\\作业\\IO流版：京东商城\\GoodsDao1.txt");
            BufferedWriter bw1=new BufferedWriter(new FileWriter(file,true));
            BufferedReader br1=new BufferedReader(new FileReader(file));
            //把lit集合的信息存入新文件中
            for (String s : list) {
                bw1.write(s);
                bw1.newLine();
            }
            //存入修改后的信息
            String goodsValue =id + "-" + goods.getGoodsName() + "-" + goods.getGoodsPrice() + "-" + goods.getGoodsColor() + "-" + goods.getGoodsSize() + "-" + goods.getStock();
            bw1.write(goodsValue);
            //将新文件的信息覆盖旧文件
            String line1=null;
            while ((line1=br1.readLine())!=null){
                bw.write(line1);
            }
            br1.close();
            bw.flush();
            bw1.close();
            br.close();
            bw.flush();
            bw.close();*/
            return true;
        }
        return false;
    }

    /**
     * 根据id删除指定商品
     *
     * @param map Goods集合
     * @param id  商品id
     * @return 返回值
     */
    @Override
    public Goods deleteById(LinkedHashMap<Integer, Goods> map, Integer id) throws IOException {
        return map.remove(id);
    }

    /**
     *  将商品对象和id写入文件
     * @param goods
     * @param id
     */
    @Override
    public void insertGoods(Goods goods, Integer id) {
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter(FILE_PATH, true));
            StringBuilder sb = new StringBuilder();
            sb.append(id);
            sb.append("-");
            sb.append(goods.getGoodsName());
            sb.append("-");
            sb.append(goods.getGoodsPrice());
            sb.append("-");
            sb.append(goods.getGoodsColor());
            sb.append("-");
            sb.append(goods.getGoodsSize());
            sb.append("-");
            sb.append(goods.getStock());
            String itemLine = sb.toString();
            bw.newLine();
            bw.write(itemLine);
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}